﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Windows.Forms;

namespace WordQuiz
{
    public partial class Form1 : Form
    {
        private WordRepository wordRepository = new WordRepository();
        private List<Word> words = new List<Word>();
        private Word currentWord;
        private int correctCount = 0;
        private int incorrectCount = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                words = wordRepository.GetAllWords();
                if (words.Count == 0)
                {
                    MessageBox.Show("当前没有单词，请先添加单词。");
                    buttonAddWord.Enabled = true;
                    textBoxInput.Enabled = true;
                }
                else
                {
                    ShowNextWord();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载单词失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 显示下一个随机单词的中文词义
        /// </summary>
        private void ShowNextWord()
        {
            if (words.Count == 0) return;
            Random random = new Random();
            currentWord = words[random.Next(0, words.Count)];
            labelQuestion.Text = currentWord.ChineseMeaning;
            textBoxInput.Text = "";
            labelResult.Text = "";
            textBoxInput.Focus();
        }

        private void textBoxInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CheckAnswer();
            }
        }

        /// <summary>
        /// 检查用户输入的英文单词是否正确
        /// </summary>
        private void CheckAnswer()
        {
            string userAnswer = textBoxInput.Text.Trim();
            if (string.IsNullOrEmpty(userAnswer))
            {
                MessageBox.Show("请输入英文单词。");
                return;
            }

            if (userAnswer.ToLower() == currentWord.EnglishWord.ToLower())
            {
                labelResult.Text = "正确！";
                correctCount++;
            }
            else
            {
                labelResult.Text = $"错误！正确答案是：{currentWord.EnglishWord}";
                incorrectCount++;
            }

            UpdateStats();
            ShowNextWord();
        }

        /// <summary>
        /// 更新统计信息（正确/错误次数和正确率）
        /// </summary>
        private void UpdateStats()
        {
            labelStats.Text = $"正确：{correctCount}，错误：{incorrectCount}，正确率：{GetAccuracy()}%";
        }

        /// <summary>
        /// 计算正确率
        /// </summary>
        private string GetAccuracy()
        {
            if (correctCount + incorrectCount == 0) return "0";
            double accuracy = ((double)correctCount / (correctCount + incorrectCount)) * 100;
            return accuracy.ToString("0.0");
        }

        private void buttonAddWord_Click(object sender, EventArgs e)
        {
            using (var addForm = new AddWordForm())
            {
                if (addForm.ShowDialog() == DialogResult.OK)
                {
                    words = wordRepository.GetAllWords();
                    if (words.Count > 0)
                    {
                        buttonAddWord.Enabled = true;
                        textBoxInput.Enabled = true;
                        ShowNextWord();
                    }
                }
            }
        }
    }
}
